Completed
Push — master ( 5f4a71...b9b3e9 )
by Roy
02:19
created

stripe-apple-pay.js ➔ jQuery   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 166

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 166
rs 8.2857
c 0
b 0
f 0
cc 1
nc 1
nop 1

5 Functions

Rating   Name   Duplication   Size   Complexity  
A stripe-apple-pay.js ➔ ... ➔ wc_stripe_apple_pay.getAjaxURL 0 5 1
A stripe-apple-pay.js ➔ ... ➔ $(document.body).updated_checkout 0 3 1
A stripe-apple-pay.js ➔ ... ➔ $(document.body).updated_cart_totals 0 3 1
A stripe-apple-pay.js ➔ ... ➔ wc_stripe_apple_pay.generate_cart 0 15 1
B stripe-apple-pay.js ➔ ... ➔ wc_stripe_apple_pay.init 0 114 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/* global wc_stripe_apple_pay_params, Stripe */
2
Stripe.setPublishableKey( wc_stripe_apple_pay_params.key );
3
4
jQuery( function( $ ) {
5
	'use strict';
6
7
	/**
8
	 * Object to handle Stripe payment forms.
9
	 */
10
	var wc_stripe_apple_pay = {
11
		/**
12
		 * Get WC AJAX endpoint URL.
13
		 *
14
		 * @param  {String} endpoint Endpoint.
15
		 * @return {String}
16
		 */
17
		getAjaxURL: function( endpoint ) {
18
			return wc_stripe_apple_pay_params.ajaxurl
19
				.toString()
20
				.replace( '%%endpoint%%', 'wc_stripe_' + endpoint );
21
		},
22
23
		/**
24
		 * Initialize event handlers and UI state.
25
		 */
26
		init: function() {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
27
			Stripe.applePay.checkAvailability( function( available ) {
28
				if ( available ) {
29
					$( '.apple-pay-button' ).show();
30
31
					wc_stripe_apple_pay.generate_cart();
32
				}
33
			});
34
35
			$( document.body ).on( 'click', '.apple-pay-button', function( e ) {
36
				e.preventDefault();
37
38
				var paymentRequest = {
39
						countryCode: wc_stripe_apple_pay_params.country_code,
40
						currencyCode: wc_stripe_apple_pay_params.currency_code,
41
						total: {
42
							label: wc_stripe_apple_pay_params.label,
43
							amount: wc_stripe_apple_pay_params.total
44
						},
45
						lineItems: wc_stripe_apple_pay_params.line_items,
46
						requiredBillingContactFields: ['postalAddress'],
47
						requiredShippingContactFields: 'yes' === wc_stripe_apple_pay_params.needs_shipping ? ['postalAddress', 'phone', 'email', 'name'] : ['phone', 'email', 'name']
48
					};
49
50
				var applePaySession = Stripe.applePay.buildSession( paymentRequest, function( result, completion ) {
51
					var data = {
52
						'nonce': wc_stripe_apple_pay_params.stripe_apple_pay_nonce,
53
						'result': result
54
					};
55
56
					$.ajax({
57
						type:    'POST',
58
						data:    data,
59
						url:     wc_stripe_apple_pay.getAjaxURL( 'apple_pay' ),
60
						success: function( response ) {
61
							if ( 'true' === response.success ) {
62
								completion( ApplePaySession.STATUS_SUCCESS );
0 ignored issues
show
Bug introduced by
The variable ApplePaySession seems to be never declared. If this is a global, consider adding a /** global: ApplePaySession */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
63
								window.location.href = response.redirect;
64
							}
65
66
							if ( 'false' === response.success ) {
67
								completion( ApplePaySession.STATUS_FAILURE );
68
69
								$( '.apple-pay-button' ).before( '<p class="woocommerce-error wc-stripe-apple-pay-error">' + response.msg + '</p>' );
70
71
								// Scroll to error so user can see it.
72
								$( document.body ).animate({ scrollTop: $( '.wc-stripe-apple-pay-error' ).offset().top }, 500 );
73
							}
74
						}
75
					});
76
				});
77
78
				// If shipping is needed -- get shipping methods.
79
				if ( 'yes' === wc_stripe_apple_pay_params.needs_shipping ) {
80
					// After the shipping contact/address has been selected
81
					applePaySession.onshippingcontactselected = function( shipping ) {
82
						var data = {
83
							'nonce': wc_stripe_apple_pay_params.stripe_apple_pay_get_shipping_methods_nonce,
84
							'address': shipping.shippingContact
85
						};
86
87
						$.ajax({
88
							type:    'POST',
89
							data:    data,
90
							url:     wc_stripe_apple_pay.getAjaxURL( 'apple_pay_get_shipping_methods' ),
91
							success: function( response ) {
92
								var total = { 
93
									'label': wc_stripe_apple_pay_params.label,
94
									'amount': response.total
95
								};
96
97
								if ( 'true' === response.success ) {
98
									applePaySession.completeShippingContactSelection( ApplePaySession.STATUS_SUCCESS, response.shipping_methods, total, response.line_items );
0 ignored issues
show
Bug introduced by
The variable ApplePaySession seems to be never declared. If this is a global, consider adding a /** global: ApplePaySession */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
99
								}
100
101
								if ( 'false' === response.success ) {
102
									applePaySession.completeShippingContactSelection( ApplePaySession.STATUS_INVALID_SHIPPING_POSTAL_ADDRESS, response.shipping_methods, total, response.line_items );
103
								}
104
							}
105
						});
106
					};
107
108
					// After the shipping method has been selected
109
					applePaySession.onshippingmethodselected = function( event ) {
110
						var data = {
111
							'nonce': wc_stripe_apple_pay_params.stripe_apple_pay_update_shipping_method_nonce,
112
							'selected_shipping_method': event.shippingMethod
113
						};
114
115
						$.ajax({
116
							type:    'POST',
117
							data:    data,
118
							url:     wc_stripe_apple_pay.getAjaxURL( 'apple_pay_update_shipping_method' ),
119
							success: function( response ) {
120
								var newTotal = {
121
									'label': wc_stripe_apple_pay_params.label,
122
									'amount': parseFloat( response.total ).toFixed(2)
123
								};
124
125
								if ( 'true' === response.success ) {
126
									applePaySession.completeShippingMethodSelection( ApplePaySession.STATUS_SUCCESS, newTotal, response.line_items );
0 ignored issues
show
Bug introduced by
The variable ApplePaySession seems to be never declared. If this is a global, consider adding a /** global: ApplePaySession */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
127
								}
128
129
								if ( 'false' === response.success ) {
130
									applePaySession.completeShippingMethodSelection( ApplePaySession.STATUS_INVALID_SHIPPING_POSTAL_ADDRESS, newTotal, response.line_items );
131
								}
132
							}
133
						});
134
					};
135
				}
136
137
				applePaySession.begin();
138
			});
139
		},
140
141
		generate_cart: function() {
142
			var data = {
143
					'nonce': wc_stripe_apple_pay_params.stripe_apple_pay_cart_nonce
144
				};
145
146
			$.ajax({
147
				type:    'POST',
148
				data:    data,
149
				url:     wc_stripe_apple_pay.getAjaxURL( 'generate_apple_pay_cart' ),
150
				success: function( response ) {
151
					wc_stripe_apple_pay_params.total      = response.total;
152
					wc_stripe_apple_pay_params.line_items = response.line_items;
153
				}
154
			});
155
		}
156
	};
157
158
	wc_stripe_apple_pay.init();
159
160
	// We need to refresh Apple Pay data when total is updated.
161
	$( document.body ).on( 'updated_cart_totals', function() {
162
		wc_stripe_apple_pay.init();
163
	});
164
165
	// We need to refresh Apple Pay data when total is updated.
166
	$( document.body ).on( 'updated_checkout', function() {
167
		wc_stripe_apple_pay.init();
168
	});
169
});
170